<script lang="ts" setup>
import type { VbenFormProps } from '@vben/common-ui';

import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';

import { Page, useVbenDrawer } from '@vben/common-ui';
import { VxeButton } from '@vben/plugins/vxe-table';

import { message } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteMachineryTypeMenu, DVMTAPI, getMachinerytypeList } from '#/api';

import { useColumns, useSearchForm } from './data';
import Form from './form.vue';

const [FormDrawer, formDrawerApi] = useVbenDrawer({
  connectedComponent: Form,
  destroyOnClose: true,
});

const formOptions: VbenFormProps = {
  // 默认展开
  collapsed: false,
  wrapperClass: 'grid-cols-2 gap-x-10 gap-y-4',
  schema: useSearchForm(),

  // 控制表单是否显示折叠按钮
  showCollapseButton: false,
  submitButtonOptions: {
    content: '搜索',
  },
  // 是否在字段值改变时提交表单
  submitOnChange: false,
  // 按下回车时是否提交表单
  submitOnEnter: false,
};

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions,
  gridOptions: {
    columns: useColumns(),
    height: 'auto',
    keepSource: true,
    pagerConfig: {
      enabled: false,
    },
    proxyConfig: {
      ajax: {
        query: async (_params, formValues) => {
          const res = await getMachinerytypeList({
            ...formValues,
          });
          return { items: res.data, total: res.data.length };
        },
      },
    },
    rowConfig: {
      keyField: 'machineryTypeId',
    },
    toolbarConfig: {
      custom: true,
      export: false,
      refresh: { code: 'query' },
      zoom: true,
    },
    treeConfig: {
      parentField: 'parentTypeId',
      rowField: 'machineryTypeId',
      transform: true,
    },
  } as VxeTableGridOptions,
});
const expandAll = () => {
  gridApi.grid?.setAllTreeExpand(true);
};

const collapseAll = () => {
  gridApi.grid?.setAllTreeExpand(false);
};

function onActionClick({ code, row }: OnActionClickParams<DVMTAPI.Datum>) {
  switch (code) {
    case 'append': {
      onAppend(row);
      break;
    }
    case 'myDel': {
      onDelete(row);
      break;
    }
    case 'myEdit': {
      onEdit(row);
      break;
    }
    default: {
      break;
    }
  }
}

function onRefresh() {
  gridApi.query();
}
function onEdit(row: DVMTAPI.Datum) {
  row.attr1 = '2';
  formDrawerApi.setData(row).open();
}
function onCreate() {
  formDrawerApi.setData({}).open();
}
function onAppend(row: DVMTAPI.Datum) {
  row.attr1 = '1';
  formDrawerApi.setData({ parentTypeId: row.machineryTypeId }).open();
}

function onDelete(row: DVMTAPI.Datum) {
  const hideLoading = message.loading({
    content: `正在删除${row.machineryTypeName}`,
    duration: 0,
    key: 'action_process_msg',
  });
  deleteMachineryTypeMenu(row.machineryTypeId)
    .then(() => {
      message.success({
        content: `删除${row.machineryTypeName}成功`,
        key: 'action_process_msg',
      });
      onRefresh();
    })
    .catch(() => {
      hideLoading();
    });
}
</script>
<template>
  <Page auto-content-height>
    <FormDrawer @success="onRefresh" />
    <Grid>
      <template #toolbar-tools>
        <VxeButton status="primary" icon="vxe-icon-add" @click="onCreate">
          新增
        </VxeButton>
        <VxeButton
          class="mx-2"
          status="primary"
          icon="vxe-icon-arrow-down"
          @click="expandAll"
        >
          展开全部
        </VxeButton>
        <VxeButton
          status="primary"
          icon="vxe-icon-arrow-right"
          @click="collapseAll"
        >
          折叠全部
        </VxeButton>
      </template>
      <template #action="{ row }">
        <VxeButton
          mode="text"
          icon="vxe-icon-signature"
          @click="onActionClick({ code: 'myEdit', row })"
          status="primary"
        >
          编辑
        </VxeButton>
        <VxeButton
          mode="text"
          icon="vxe-icon-add"
          @click="onActionClick({ code: 'append', row })"
          status="warning"
        >
          新增
        </VxeButton>
        <VxeButton
          v-if="row.machineryTypeId !== 1"
          icon="vxe-icon-delete"
          mode="text"
          @click="onActionClick({ code: 'myDel', row })"
          status="error"
        >
          删除
        </VxeButton>
      </template>
    </Grid>
  </Page>
</template>
<style lang="scss" scoped>
.menu-badge {
  top: 50%;
  right: 0;
  transform: translateY(-50%);

  & > :deep(div) {
    padding-top: 0;
    padding-bottom: 0;
  }
}
</style>
